package com.health.mapp.impl;

import com.health.db.model.UserCashAccountIdNo;
import com.health.db.model.UserCashAccountIdNoExample;
import com.health.db.model.UserCashAccountIdNoExample.Criteria;
import com.health.db.model.UserLIST;
import com.health.db.service.user.IUserCashAccountIdNoService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.RemoveAccountReq;
import com.health.mapp.bean.RemoveAccountResp;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

import java.util.List;

/**
 * Created by YYY-Work on 2017/5/31.
 */
public class RemoveAccountImpl implements ActionInterface {

    private IUserCashAccountIdNoService userCashAccountIdNoService;
    private IUserLISTService userLISTService;

    @Override
    public Response execute(Header header, Object reqBean) throws Exception {

        RemoveAccountReq req = (RemoveAccountReq) reqBean;
        RemoveAccountResp _resp = new RemoveAccountResp();

        if (null == req.getUserId() || null == req.getAccountId()) {
            _resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
            _resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
            return _resp;
        }

        UserLIST user = userLISTService.selectByUserId(req.getUserId());
        if (null != user) {

            UserCashAccountIdNoExample example=new UserCashAccountIdNoExample();
            Criteria criteria=example.createCriteria();
            criteria.andOwneridEqualTo(req.getUserId());
            criteria.andAccountidEqualTo(req.getAccountId());
            List<UserCashAccountIdNo> userCashAccountIdNos=userCashAccountIdNoService.queryUserCashAccountIdNo(example);
            if (null!=userCashAccountIdNos||userCashAccountIdNos.size()>0)
            {

                UserCashAccountIdNo userCashAccountIdNo=userCashAccountIdNos.get(0);
                userCashAccountIdNo.setIsUsedFlag((short) 0);
                userCashAccountIdNoService.updateUserCashAccountIdNo(userCashAccountIdNo);

                _resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
                _resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
                return _resp;

            }else
            {
                _resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_5004);
                _resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_5004_NOTE);
                return _resp;
            }

        } else {
            /**
             * 存在此账户类型 不能再添加
             * */
            _resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_5000);
            _resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_5000_NOTE);
            return _resp;
        }

    }

    public IUserCashAccountIdNoService getUserCashAccountIdNoService() {
        return userCashAccountIdNoService;
    }

    public void setUserCashAccountIdNoService(IUserCashAccountIdNoService userCashAccountIdNoService) {
        this.userCashAccountIdNoService = userCashAccountIdNoService;
    }

    public IUserLISTService getUserLISTService() {
        return userLISTService;
    }

    public void setUserLISTService(IUserLISTService userLISTService) {
        this.userLISTService = userLISTService;
    }
}
